'use strict';

var _ = require('underscore');
var bcrypt = require('bcryptjs');
var Promise = require('bluebird');

function encodePassword(user) {
    return new Promise(function(resolve, reject) {
        if (!user.password) {
            return resolve(user);
        } else {
            if (user.changed('password')) {
                //对密码进行加密
                bcrypt.genSalt(10, function(err, salt) {
                    if (err) {
                        return reject(err);
                    }
                    bcrypt.hash(user.password, salt, function(err, hash) {
                        if (err) {
                            return reject(err);
                        }
                        user.password = hash;
                        return resolve(user);
                    });
                });
            } else {
                return resolve(user);
            }
        }
    });
};

module.exports = function(sequelize, DataTypes) {
    //用户表
    var User = sequelize.define('User', {
        //登录名
        loginName: {
            type: DataTypes.STRING(30),
            allowNull: false,
            unique: true,
            comment: '登录名',
            validate: {
                notEmpty: {
                    msg: '不允许为空'
                },
                len: {
                    args: [1, 30],
                    msg: '长度不允许超过30'
                }
            }
        },
        //密码
        password: {
            type: DataTypes.STRING,
            allowNull: false,
            comment: '密码',
            validate: {
                notEmpty: {
                    msg: '不允许为空'
                }
            }
        },
        //昵称
        alias: {
            type: DataTypes.STRING,
            allowNull: false,
            comment: '昵称',
            validate: {
                notEmpty: {
                    msg: '不允许为空'
                }
            }
        },
        //邮箱
        email: {
            type: DataTypes.STRING,
            allowNull: false,
            comment: '邮箱',
            validate: {
                notEmpty: {
                    msg: '不允许为空'
                }
            }
        },
        //头像地址
        headImg: {
            type: DataTypes.STRING,
            comment: '头像地址'
        }
    }, {
        charset: 'utf8',
        classMethods: {
            associate: function(models) {

            },
            fetch: function(op) {
                return this.findAll(_.extend({
                    order: [
                        ['createdAt', 'DESC']
                    ]
                }, op || {}));
            },
            findByLoginName: function(loginName) {
                return this.find({
                    where: {
                        loginName: loginName
                    },
                    raw: true
                });
            }
        },
        instanceMethods: {
            comparePassword: function(inputpw) {
                var user = this.get({ plain: true });
                return new Promise(function(resolve, reject) {
                    bcrypt.compare(inputpw, user.password, function(err, isMatch) {
                        if (err) {
                            return reject(err);
                        }
                        if (isMatch) {
                            return resolve(user);
                        } else {
                            return reject(new Error('用户名或密码不正确！'));
                        }
                    });
                });
            }
        },
        hooks: {
            beforeCreate: function(user, options) {
                return encodePassword(user);
            },
            beforeUpdate: function(user, options) {
                return encodePassword(user);
            }
        }
    });

    return User;
};
